package safari.model.buy;

import java.sql.SQLException;
import java.util.List;

import safari.model.common.ListVO;
import safari.model.common.PagingBean;

public class BuyBoardServiceImpl implements BuyBoardService {
	private BuyBoardDao buyBoardDao;

	public BuyBoardServiceImpl(BuyBoardDao buyBoardDaodao) {
		this.buyBoardDao = buyBoardDaodao;
	}
	
	/* 게시물 쓰기*/
	@Override
	public int insertContent(BuyBoardVO bvo) throws SQLException {
		return buyBoardDao.insertContent(bvo);
	}
	
	/* 게시물 등록 */
	@Override
	public BuyBoardVO register(BuyBoardVO bvo) throws SQLException {
		buyBoardDao.insertContent(bvo);
		/*int retVal= buyBoardDaodao.showNo();
		System.out.println(retVal);*/
		/*BuyBoardVO b =buyBoardDaodao.showContent(buyBoardDaodao.showNo());
		System.out.println(b);*/
		System.out.println("서비스 게시물등록:"+bvo);
		return buyBoardDao.showContent(bvo.getNo());
	}
	 
	/* 댓글 달기 
	@Override
	public List<BuyCommentVO> registerComment(BuyCommentVO bcvo) throws SQLException {
		int result = buyBoardDao.insertComment(bcvo);
		System.out.println("서비스 댓글등록: "+bcvo+" 결과:"+result);
		System.out.println(buyBoardDao.getCommentList(bcvo.getNo()));
		System.out.println("서비스 댓글달기 결과:"+result);
		return getCommentList(bcvo.getBuyBoardVO().getNo()); 
		//bcvo.getNo() 리스트에 계속 널[] 나옴. -> 해결:bcvo.getBuyBoardVO().getNo()를 넣었더니 커멘트 no를 넣어서 null 나옴.
	}*/
	
	/* 댓글 한 개 달기 */ 
	@Override
	public BuyCommentVO registerComment(BuyCommentVO bcvo) throws SQLException {
		int result = buyBoardDao.insertComment(bcvo);
		System.out.println("서비스 댓글등록: "+bcvo+" 결과:"+result);
		/*System.out.println(buyBoardDao.getComment(bcvo.getBuyBoardVO().getNo()));*/
		System.out.println("서비스 댓글달기 결과:"+result);
		return buyBoardDao.getComment(bcvo.getBuyBoardVO().getNo());
	}
	
	
	/* 댓글 리스트 보여주기 (목록에서 글제목을 눌러서 글내용 볼 때 발생)*/
	@Override
	public List<BuyCommentVO> getCommentList(int bno) throws SQLException{
		System.out.println("댓글보기 서비스:"+buyBoardDao.getCommentList(bno));
		return buyBoardDao.getCommentList(bno);
	}
	
	/* 댓글목록 */
	/*@Override
	public ListVO getCommentList(int nowPage) throws SQLException{
		List<BuyCommentVO> list = buyBoardDao.getCommentList(nowPage);
		int totalComments = buyBoardDao.totalComments();
		PagingBean p = null;
		if(nowPage==0){
			p = new PagingBean(totalComments);
		}else{
			p = new PagingBean(totalComments, nowPage);
		}
		return new ListVO(list,p);
	}*/

	/* 게시물 조회, 이 때 조회수 증가 */
	@Override
	public BuyBoardVO showContentHits(int no) throws SQLException {
		buyBoardDao.updateHits(no);
		return buyBoardDao.showContent(no);
	}
	
	/* 게시물 조회, 이 때 조회수 증가하지 않음 */
	@Override
	public BuyBoardVO showContentNoHits(int no) throws SQLException {
		return buyBoardDao.showContent(no);
	}
	
	/* 총 개수 */
	@Override
	public int totalContents() throws SQLException {
		return buyBoardDao.totalContents();
	}
	
	/* 목록 */
	@Override
	public ListVO getContentList(int nowPage) throws SQLException {
		List<BuyBoardVO> list = buyBoardDao.getContentList(nowPage);
		int totalContents = buyBoardDao.totalContents();
		PagingBean p = null;
		if(nowPage==0){
			p = new PagingBean(totalContents);
		}else{
			p = new PagingBean(totalContents, nowPage);
		}
		return new ListVO(list,p);
	}
	
	/* 수정 */
	@Override
	public BuyBoardVO updateContent(BuyBoardVO bvo) throws SQLException {
		this.buyBoardDao.updateContent(bvo);
		System.out.println("서비스 수정:"+bvo);
		return buyBoardDao.showContent(bvo.getNo());
	}
	/* 삭제  */
	@Override
	public int deleteContent(int no) throws SQLException {
		return buyBoardDao.deleteContent(no);
	}
	
	/* 댓글 수정 */
	@Override
	public BuyCommentVO updateComment(BuyCommentVO bcvo) throws SQLException {
		 buyBoardDao.updateComment(bcvo);
		 return null;
	}
	/* 댓글 삭제 */
	@Override
	public int deleteComment(int no) throws SQLException {
		return buyBoardDao.deleteComment(no);
	}

	
}
